<?php
class Surats extends CI_Model{
	function getData($kap,$no='',$client='',$start='',$end=''){
		$sql = " SELECT * FROM surats ";
		$sql.= " WHERE kap='".$kap."'";
		if(!empty($no)):
			$sql.= " AND no_surat LIKE '%".$no."%' ";
		endif;
		if(!empty($client)):
			$sql.= " AND nama_klien LIKE '%".$client."%' ";
		endif;
		if(!empty($start)):
			$sql.= " AND tanggal >='".$start."' AND tanggal<='".$end."'";
		endif;
		$sql.= " ORDER BY id DESC ";
		$q=$this->db->query($sql);
		if($q->num_rows()>0) {
			return $q->result_array();
			$q->free_result();
		}
	}
	
	function getLatestPK($kap,$id){
		$sql = " SELECT ".$id." FROM surats ";
		$sql.= " WHERE kap='".$kap."'";
		$sql.= " AND YEAR(tanggal)='".date('Y')."'";
		$sql.= " ORDER BY ABS(".$id.") DESC LIMIT 1 ";
		//echo $sql;
		$q=$this->db->query($sql);
		$row=$q->row_array();
		if($q->num_rows()>0):
			
				//$no=1;
			//if($row>=174){
				//$data = 1;
			//}
			//else{
				$data=$row['no']+1;
			//}
			if($data<=9):
				$data = '00'.$data;
			elseif(($data>=10) AND ($data<=99) ):
				$data = '0'.$data;
			elseif(($data>=99) AND ($data<=999) ):
				$data = $data;
			endif;	
		else:	
			$data='001';
		endif;
		return $data;
		
	}
	
	function getSave($kap){
		$no = $this->input->post('no').'/'.$this->input->post('type').'/'.$this->input->post('tujuan').'/'.$kap.'/'.to_romawi($this->input->post('month')).'/'.substr($this->input->post('year'),2,2);
		$val=array(
			'no_surat' 	=>  $no,
			'no'	   	=>  $this->input->post('no'),
			'type'	   	=>  $this->input->post('type'),
            'tujuan'   	=>  $this->input->post('tujuan'),
			'kap'   	=>  $this->input->post('kap'),
			'tanggal'  	=>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
			'nama_klien'=>	$this->input->post('nama_klien'),
			'alamat'	=>	$this->input->post('alamat'),
			'keterangan'=>	$this->input->post('keterangan'),
			'pembuat'	=>	$this->session->userdata('name'),
			'is_trash'	=>	0	
		);
		$this->db->insert('surats',$val);	
	}
	
	function getID($id){
		$sql = " SELECT *,DAY(tanggal) as day,MONTH(tanggal) as month,YEAR(tanggal) as year FROM surats ";
		$sql.= " WHERE id = ".$id;
		$q=$this->db->query($sql);
		return $q->row_array();
		$q->free_result();
	}
	
	function getUpdate($kap){
		$no = $this->input->post('no').'/'.$this->input->post('type').'/'.$this->input->post('tujuan').'/'.$kap.'/'.to_romawi($this->input->post('month')).'/'.substr($this->input->post('year'),2,2);
		$val=array(
			'no_surat' 	=>  $no,
			'no'	   	=>  $this->input->post('no'),
			'type'	   	=>  $this->input->post('type'),
            'tujuan'   	=>  $this->input->post('tujuan'),
			'tanggal'  	=>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
			'nama_klien'=>	$this->input->post('nama_klien'),
			'alamat'	=>	$this->input->post('alamat'),
			'keterangan'=>	$this->input->post('keterangan'),
			'pembuat'	=>	$this->session->userdata('name'),
			'is_trash'	=>	0	
		);
		$this->db->where('id',$this->input->post('id'));
		$this->db->update('surats',$val);	
	}
	
	function getRemove($id){
		$this->db->where('id',$id);
		$this->db->delete('surats');
	}
	
}